<template>
  <Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
    <FormItem prop="userName">
      <Input v-model="form.userName" placeholder="请输入用户名">
        <span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="password">
      <Input type="password" v-model="form.password" placeholder="请输入密码">
        <span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
      </Input>
    </FormItem>
    <FormItem prop="captcha" v-if="LOGIN_CAPTCHA_CHECK">
      <Row :gutter="16">
        <Col span="15">
          <Input type="text" v-model="form.captcha" placeholder="请输入验证码">
          </Input>
        </Col>
        <Col span="9"><img :src="imgPath" @click="change" style="width: 100px"></Col>
      </Row>
    </FormItem>
    <FormItem>
      <Button :loading="loading" @click="handleSubmit" type="primary" long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>
import config from '@/config'
import { uuid } from '../../libs/util'
import { getSysParam } from '../../api/sys/param'
import { getAddress } from '@/api/sys/login'

const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro

export default {
  name: 'LoginForm',
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    },
    captchaRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  data () {
    return {
      loading: false,
      LOGIN_CAPTCHA_CHECK: false,
      address: {},
      form: {
        uuid: '',
        userName: '',
        password: ''
      }
    }
  },
  computed: {
    imgPath () {
      return baseUrl + 'captcha.jpg?uuid=' + this.form.uuid
    },
    rules () {
      return {
        userName: this.userNameRules,
        password: this.passwordRules,
        captcha: this.captchaRules
      }
    }
  },
  methods: {
    change () {
      this.form.uuid = uuid().replace('-', '')
    },
    initLoading (loading) {
      this.loading = loading
    },
    handleSubmit () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$emit('on-success-valid', {
            uuid: this.form.uuid,
            captcha: this.form.captcha,
            userName: this.form.userName,
            password: this.form.password,
            address: this.address
          })
        }
      })
    }
  },
  mounted () {
    getAddress(this)
    this.change()
    getSysParam('LOGIN_CAPTCHA_CHECK').then(res => {
      this.LOGIN_CAPTCHA_CHECK = res === 'Y'
    })
  }
}
</script>
